<?php

namespace App\Admin\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('列表')
            ->description('用户')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.detail'))
            ->description(trans('admin.description'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.edit'))
            ->description(trans('admin.description'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.create'))
            ->description(trans('admin.description'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->id('ID');
        $grid->column('avatar')->image(60, 60);
        $grid->name('用户名')->editable()->filter();
        $grid->point('积分')->editable()->sortable();
        $grid->immune('免黑')->help('根据Y,Ym,Ymd规则免黑')->editable();
        $grid->openid('OpenID')->hide();
        $grid->fromid('FromID')->hide()->editable();
        $grid->gender('性别')->editable();
        $grid->city('城市')->editable();
        $grid->bio('个性签名')->hide()->editable();
        $grid->lock_at('锁定时间')->hide();
        $grid->authorize('授权发帖')->switch()->hide();
        $grid->liked('受赞')->editable()->sortable();
        $grid->total_point('总积分')->editable()->hide()->sortable();
        $grid->linkman('联系人')->editable()->filter()->hide();
        $grid->mobile('手机号')->editable()->filter()->hide();
        $grid->wechat('微信号')->editable()->filter()->hide();
        $grid->pre_video_adid('前帖视频')->editable()->filter()->hide()->help('播放视频前');
        $grid->video_adid('视频广告')->editable()->filter()->hide()->help('详情页固定广告不要与banner广告同用');
        $grid->banner_adid('banner广告')->editable()->filter()->hide()->help('详情页固定广告不要与视频广告同用');
        $grid->screen_adid('插屏广告')->editable()->filter()->hide()->help('复制链接时使用');
        $grid->reward_adid('激励式视频')->editable()->filter()->hide()->help('获取密令时使用');
        $grid->created_at(trans('admin.created_at'))->hide();
        $grid->updated_at(trans('admin.updated_at'))->hide();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('ID');
        $show->user_id('user_id');
        $show->check_id('check_id');
        $show->rank('rank');
        $show->liked('liked');
        $show->body('body');
        $show->images('images');
        $show->category_id('category_id');
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->display('ID');
        $form->text('name', '用户名');
        $states = [
            'on'  => ['value' => 1, 'text' => '授权', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁止', 'color' => 'danger'],
        ];
        $form->switch('authorize', '发帖')->states($states);
        $form->text('linkman', '联系人');
        $form->text('mobile', '手机号');
        $form->text('wechat', '微信号');
        $form->text('point', '积分');
        $form->text('total_point', '总积分');
        $form->text('immune', '免黑')->help('根据Y,Ym,Ymd规则免黑');
        $form->text('openid', 'OpenID');
        $form->text('fromid', 'FromID');
        $form->text('gender', '性别');
        $form->text('city', '城市');
        $form->image('avatar', '头像');
        $form->textarea('bio', '个性签名');
        $form->datetime('lock_at', '锁定时间')->format('YYYY-MM-DD HH:mm:ss');
        $form->text('liked', '受赞');
        $form->text('pre_video_adid', '前帖视频')->help('播放视频前');
        $form->text('video_adid', '视频广告')->help('详情页固定广告不要与banner广告同用');
        $form->text('banner_adid', 'banner广告')->help('详情页固定广告不要与视频广告同用');
        $form->text('screen_adid', '插屏广告')->help('复制链接时使用');
        $form->text('reward_adid', '激励式视频')->help('获取密令时使用');

        $form->display(trans('admin.created_at'));
        $form->display(trans('admin.updated_at'));

        return $form;
    }
}
